草庐IT

Lua 数据类型

全部标签

testing - 转到类型错误 : struct does not implement interface

这个问题在这里已经有了答案:Structdoesnotimplementinterfaceifithasafunctionwhichparameterimplementinterface(2个回答)2年前关闭。//BEGIN:externallibrarytyperealXstruct{}typerealYstruct{}func(realX)Do()realY{returnrealY{}}//ENDtypeAstruct{amyX}typemyYinterface{}typemyXinterface{Do()myY}funcfoo(arg1myY){}funcmain(){foo(r

go - 如何使用 Golang 获取发布的二进制数据的文件名?

我使用PostmanRESTAPI客户端将一个csv文件作为二进制文件发布。我需要获取上传文件的文件名。这是一个将csv文件作为二进制数据发布并将二进制数据存储为csv文件的简单示例。packagemainimport(//"fmt""net/http""os""io""log""github.com/gorilla/mux")funcuploadData(whttp.ResponseWriter,req*http.Request){file,err:=os.Create("hello.csv")_,err=io.Copy(file,req.Body)_=err}funcmain(){

arrays - 如何使用 Golang 将数据放入结构中?

这是我的代码:packagemainimport"fmt"typeSpeciesstruct{Human[]InfoAnimal[]Info}typeInfostruct{NamestringNumberstring}funcmain(){vardataSpeciesdata=????fmt.Println(data)}我想把它看成这样的json:{"human":[{"name":"dave","number":"00001"},{"name":"jack","number":"00002"},{"name":"nate","number":"00003"}],"animal":[{

pointers - 从非 chan 类型接收 *bool

我想守护我的应用程序,但我有一个大问题。我使用的channel是chanstruct{}类型。但是,对于包getopt(标志包),我的标志是*bool类型,所以我不知道如何修改我的应用程序。bool类型的channel还不够。我确定有一个我不明白的概念。我附上代码:packagemainimport("os""syscall""time""github.com/pborman/getopt/v2""github.com/sevlyar/go-daemon")var(done=make(chanstruct{})optQuit=make(chanstruct{})optRun=make(

string - 无法在字符串 : "cannot use <xxx> (type <yyy>) as type string in map index" 的映射中使用基础类型的字符串

这个问题在这里已经有了答案:ConvertingacustomtypetostringinGo(4个答案)关闭3年前。我有底层字符串类型:typeCapabilitystring。我想将它用作字符串映射中的字符串,但出现错误:cannotusecap(typeCapability)astypestringinmapindex这是我的代码:packagemainimport("fmt")typeCapabilitystringvarcaps_list=map[string]int{"HOME":1,}funcmain(){varcapCapability//stringcap="HOME

database - 如何在 Go DRY 中扫描数据库行?

我在数据库中有一个包含用户帐户信息的表。我有一个名为用户定义的结构。typeUserstruct{IduintUsernamestringPasswordstringFirstNamestringLastNamestringAddress1stringAddress2string....abunchmorefields...}为了获取个人用户帐户,我定义了一个方法func(user*User)GetById(db*sql.DB,iduint)error{query:=`SELECT...awholebunchofSQL...WHEREid=$1...moreSQL...LIMIT1`r

go - 是否有必要明确提及变量的类型?

Closed.Thisquestionisopinion-based。它当前不接受答案。想改善这个问题吗?更新问题,以便editingthispost用事实和引用来回答。2年前关闭。Improvethisquestion我听说在声明变量时定义变量类型总是好的,但是无论我在哪里看到Go代码,无论是任何包,库还是博客,我都发现了不提及类型的定义变量的简便方法。 最佳答案 不必总是写变量的类型。Go具有:=运算符的类型推断,但有时您必须声明它。您可以通过两种方式声明变量:varageintage:=12在第一个变量上,您声明一个名为age

go - 如何根据我发送给 Go 中的函数的变量返回特定类型的 slice

我有一个函数,它接受一个空接口(interface)(任何类型,我正在寻找特定的2),然后返回所选类型的一部分。functestingInterface(tempinterface{})(interface{},interface{}){vardocinterface{}array:=make([]interface{},3)switchx:=temp.(type){caseint:doc=xtempArray:=make([]string,3)fori,v:=rangetempArray{array[i]=string(v)}fmt.Printf("Inttostring%T,%T"

go - 如何查找类型是否为 float64

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭3年前。Improvethisquestion我正在尝试查找变量是否为float64类型:packagemainimport("fmt")funcmain(){myvar:=12.34ifmyvar.(type)==float64{fmt.Println("Typeisfloat64.")}}但是,它不工作并给出以下错误:./rnFindType.go:6:10:useof.(type)outsidetypeswitch./rnFindTyp

go - 未捕获类型断言中的第二个返回值会导致运行时 panic

关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭4年前。Improvethisquestion如果断言失败并且没有为返回值的分配提供第二个值“OK”,下面的代码会导致第二个Println出现运行时panic。但是,如果提供了第二个值,则不会发生运行时panic。不分配返回值怎么会导致panic?有什么好的资源可以了解Go中的panic吗?variinterface{}="hello"f,ok:=i.(float64)//noruntimepanicfmt.Println(f,ok)f=i.